<template>
  <el-main>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/device' }"
        >设备管理
      </el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/device/trashcan/location' }"
        >垃圾箱位置
      </el-breadcrumb-item>
      <el-breadcrumb-item>修改垃圾箱位置</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="margin-top-20">
      <el-card shadow="never">
        <el-form :model="form" :rules="rules" label-width="120px" ref="form">
          <el-form-item
            class="m-input-400"
            label="位置名称"
            prop="locationName"
          >
            <el-input autocomplete="off" v-model="form.locationName" />
          </el-form-item>
          <el-form-item
            class="m-input-400"
            label="垃圾箱类型"
            prop="trashcanType"
          >
            <el-input autocomplete="off" v-model="form.trashcanType" />
          </el-form-item>
          <el-form-item class="m-input-400" label="垃圾箱容量" prop="volume">
            <el-input autocomplete="off" v-model="form.volume">
              <template slot="append"
                >L
              </template>
            </el-input>
          </el-form-item>
          <el-form-item class="m-input-400" label="垃圾箱经度" prop="lng">
            <el-input autocomplete="off" v-model="form.lng"> </el-input>
          </el-form-item>
          <el-form-item class="m-input-400" label="垃圾箱纬度" prop="lat">
            <el-input autocomplete="off" v-model="form.lat"> </el-input>
          </el-form-item>
          <el-form-item class="m-input-400" label="备注" prop="remarks">
            <el-input type="textarea" v-model="form.remarks"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="submitForm('form')" type="primary"
              >立即修改
            </el-button>
            <el-button @click="cancelForm">取消</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </el-main>
</template>

<script>
export default {
  data() {
    return {
      id: null,
      form: {
        locationName: null,
        trashcanType: null,
        volume: null,
        lng: null,
        lat: null,
        remarks: null
      },
      rules: {
        locationName: [
          { required: true, message: "请输入垃圾箱名称", trigger: "none" }
        ],
        trashcanType: [
          { required: true, message: "请输入垃圾箱类型", trigger: "none" }
        ],
        volume: [
          { required: true, message: "请输入垃圾箱容量", trigger: "none" }
        ],
        lng: [{ required: true, message: "请输入垃圾箱经度", trigger: "none" }],
        lat: [{ required: true, message: "请输入垃圾箱纬度", trigger: "none" }]
      }
    };
  },
  mounted() {
    this.id = this.$route.params.id;
    this.info();
  },
  methods: {
    info() {
      this.$ajax.trashcan_location
        .info(this.id)
        .then(result => {
          if (result.code == 1) {
            this.form = result.data;
          } else {
            this.$message({
              message: result.msg,
              type: "warning"
            });
          }
        })
        .catch(e => {
          console.log(e);
        });
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          let loadingInstance = this.$loading({ fullscreen: true });
          this.$ajax.trashcan_location
            .update(this.form)
            .then(result => {
              if (result.code != 1) {
                this.$message({
                  message: result.msg,
                  type: "warning"
                });
              } else {
                this.$message({
                  type: "success",
                  message: "修改垃圾箱位置成功!"
                });
                this.$router.push("/device/trashcan/location");
              }
            })
            .catch(e => {
              console.log(e);
              this.$message({
                message: "系统繁忙，请稍后重试",
                type: "warning"
              });
            })
            .finally(function() {
              loadingInstance.close();
            });
        } else {
          return false;
        }
      });
    },
    cancelForm() {
      this.$router.push("/device/trashcan/location");
    }
  }
};
</script>
